package com.algrithom.pointer;

import java.util.Objects;

import com.common.model.ListNode;

/**
 * 876. 链表的中间结点
 * 
 * @author think
 */
public class Solution8 {
    
    public static void main(String[] args){
        ListNode<Integer> root = new ListNode<>(1);
        root.setNext(new ListNode<>(2));
        root.getNext().setNext(new ListNode<>(3));
        root.getNext().getNext().setNext(new ListNode<>(4));
        root.getNext().getNext().getNext().setNext(new ListNode<>(5));
        root.getNext().getNext().getNext().getNext().setNext(new ListNode<>(6));
        Solution8 solution8 = new Solution8();
        System.out.println(solution8.middleNode(root).getVal());
    }
    
    public ListNode<Integer> middleNode(ListNode<Integer> head){
        ListNode<Integer> slow = head;
        ListNode<Integer> fast = head;
        while (Objects.nonNull(fast) && Objects.nonNull(fast.getNext())) {
            slow = slow.getNext();
            fast = fast.getNext().getNext();
        }
        return slow;
    }
}
